﻿using AutoMapper;
using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Process_Procedures.ProcessManagement;
using ReturnValue;
using Yitter.IdGenerator;

namespace MES.Server.API.Application.Handler.Process_Procedures.ProcessManagement
{
    public class CreateProcessManagementHandler : IRequestHandler<CreateProcessManagementCommand, ApiResult<int>>
    {
        private readonly IRepository<ProcessManagementModel> processmanRepo;
        private readonly IMapper mapper;

        public CreateProcessManagementHandler(IRepository<ProcessManagementModel> processmanRepo,IMapper mapper)
        {
            this.processmanRepo = processmanRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(CreateProcessManagementCommand request, CancellationToken cancellationToken)
        {
            var model= mapper.Map<ProcessManagementModel>(request);
            model.Id=YitIdHelper.NextId();
            var r=await processmanRepo.AddAsync(model);
            if (r > 0)
            {
                return new ApiResult<int>().AddSuccess("工序管理", r);
            }
            else
            {
                return new ApiResult<int>().AddFail("工序管理");
            }
        }
    }
}
